<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="名称" clearable  class="w165"></el-input>
                    <el-select v-model="search.categoryId" placeholder="类型" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.id" v-for="(item,index) in categoryData" :key="index"></el-option>
					</el-select>
                    <el-select v-model="search.levelId" placeholder="学段" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.id" v-for="(item,index) in levelData" :key="index"></el-option>
					</el-select>
                    <el-select v-model="search.difficultyId" placeholder="难度" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.id" v-for="(item,index) in difficultyData" :key="index"></el-option>
					</el-select>
                    <el-select v-model="search.bookId" placeholder="教材" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.id" v-for="(item,index) in bookData" :key="index"></el-option>
					</el-select>
                    <el-select v-model="search.isTrial" placeholder="体验课" clearable>
                        <el-option label="是" :value="1"></el-option>
                        <el-option label="否" :value="0"></el-option>
                    </el-select>
                    <el-select v-model="search.isRandom" placeholder="词汇随机" clearable>
                        <el-option label="是" :value="1"></el-option>
                        <el-option label="否" :value="0"></el-option>
                    </el-select>
                    <el-select v-model="search.status" placeholder="状态" clearable>
                        <el-option label="启用" :value="1"></el-option>
                        <el-option label="禁用" :value="0"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-plus" @click="table_add" v-if="permission.add">增加</el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.course.list" remoteSort @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="名称" show-overflow-tooltip prop="name" width="200" align="center"></el-table-column>
                    <el-table-column label="类型" prop="category" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.category  ? scope.row.category.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="学段" prop="level" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.level  ? scope.row.level.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="难度" prop="difficulty" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.difficulty  ? scope.row.difficulty.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="教材" prop="book" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.book  ? scope.row.book.name : ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="词汇数量" prop="words" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="体验课" prop="isTrial" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.isTrial.name || ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="词汇随机" prop="isRandom" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.isRandom.name || ''}}
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status" width="120" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.status.value == 0" type="danger">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 1" type="success">{{scope.row.status.name}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="220">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
                            <el-divider v-if="permission.edit" direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="table_edit(scope.row, scope.$index)" v-if="permission.edit">编辑</el-button>
                            <el-divider v-if="permission.vocabulary && scope.row.category.id == 1" direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="table_vocabulary(scope.row)" v-if="permission.vocabulary && scope.row.category.id == 1">词库管理</el-button>
                            <el-divider v-if="permission.article && scope.row.category.id == 4" direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="table_article(scope.row)" v-if="permission.article && scope.row.category.id == 4">文章管理</el-button>
                            <el-divider v-if="permission.article && scope.row.category.id == 8" direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="table_chapter(scope.row)" v-if="permission.article && scope.row.category.id == 8">词串章节</el-button>
                            <el-divider v-if="scope.row.category.id == 10" direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="grammar_Topic(scope.row)" v-if="scope.row.category.id == 10">语法专题</el-button>
                          <el-divider v-if="scope.row.category.id == 11" direction="vertical"></el-divider>
                          <el-button type="text" size="small" @click="content_Topic(scope.row)" v-if="scope.row.category.id == 11">内容管理</el-button>
                            <el-divider v-if="permission.vocabulary && scope.row.category.id == 9" direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="table_sentence(scope.row)" v-if="permission.vocabulary && scope.row.category.id == 9">句子管理</el-button>
                            <el-divider direction="vertical" v-if="permission.delete"></el-divider>
                            <el-popconfirm :title="'确认是否要删除课程：' + scope.row.name + '?'" @confirm="table_del(scope.row)" v-if="permission.delete">
                                <template #reference>
                                    <el-button type="text" size="small" v-if="permission.delete">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" :categoryData="categoryData" :levelData="levelData" :difficultyData="difficultyData" :bookData="bookData" @closed="dialog.save=false"></save-dialog>

    <vocabulary-dialog v-if="dialog.vocabulary" ref="vocabularyDialog" @success="handleSuccess" @closed="handVocabularyClose"></vocabulary-dialog>

    <article-dialog v-if="dialog.article" ref="articleDialog" @success="handleSuccess" @closed="dialog.article=false"></article-dialog>

    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>

    <chapter-dialog v-if="dialog.chapter" ref="chapterDialog" @success="handleSuccess" @closed="dialog.chapter=false"></chapter-dialog>

    <sentence-dialog v-if="dialog.sentence" ref="sentenceDialog" @success="handleSuccess" @closed="dialog.sentence=false"></sentence-dialog>

    <grammar-dialog v-if="dialog.grammar" ref="grammarDialog" @success="handleSuccess" @closed="dialog.grammar=false"></grammar-dialog>

    <content-dialog v-if="dialog.content" ref="contentDialog" @success="handleSuccess" @closed="dialog.content=false"></content-dialog>

</template>

<script>
import { rolePermission } from '@/utils/permission'
import saveDialog from './save'
import vocabularyDialog from './vocabulary'
import articleDialog from './article'
import detailDialog from './detail'
import chapterDialog from './chapter'
import sentenceDialog from './sentence'
import grammarDialog from './grammar'
import contentDialog from "./content";
export default {
    components: {
        saveDialog,
        vocabularyDialog,
        detailDialog,
        articleDialog,
        chapterDialog,
        grammarDialog,
       contentDialog,
        sentenceDialog
    },
    data() {
        return {
            dialog: {
                detail: false,
                save: false,
                grammar:false,
                vocabulary: false,
                article: false,
                chapter: false,
                sentence: false,
                content: false,
            },
            search: {
                name: null,
            },
            permission: {
				add: rolePermission('course-list-add'),
				edit: rolePermission('course-list-edit'),
				delete: rolePermission('course-list-delete'),
                vocabulary: rolePermission('course-vocabulary-view'),
                article: rolePermission('course-article-view'),
			},
            categoryData: [],
            levelData: [],
            difficultyData: [],
            bookData: [],
        }
    },
    mounted() {
        this.getAllCategory()
        this.getAllLevel()
        this.getAllDifficulty()
        this.getAllBook()
    },
    methods: {
      content_Topic(row){
        this.dialog.content = true
        console.log(this.dialog.content)
        this.$nextTick(() => {
          this.$refs.contentDialog.open(row)
        })
        console.log(this.$refs)
      },
      grammar_Topic(row){
        this.dialog.grammar = true
        this.$nextTick(() => {
          this.$refs.grammarDialog.open(row)
        })
      },
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        getAllCategory() {
            this.$API.course.category.all.get().then(res => {
                this.categoryData = res.data
            })
        },
        getAllLevel() {
            this.$API.course.level.all.get().then(res => {
                this.levelData = res.data
            })
        },
        getAllDifficulty() {
            this.$API.course.difficulty.all.get().then(res => {
                this.difficultyData = res.data
            })
        },
        getAllBook() {
            this.$API.course.book.all.get().then(res => {
                this.bookData = res.data
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id)
			})
        },
        //添加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        // 短句管理
        table_sentence(row) {
            this.dialog.sentence = true
            this.$nextTick(() => {
                this.$refs.sentenceDialog.open(row)
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //删除
        async table_del(row) {
            await this.$API.course.action.delete({ id: row.id }).then(() => {
                this.$refs.table.refresh()
                this.$notify.success("删除成功")
            });
        },
        //词库管理
        table_vocabulary(row) {
			this.dialog.vocabulary = true
			this.$nextTick(() => {
				this.$refs.vocabularyDialog.open(row)
			})
		},
        //阅读文章管理
        table_article(row) {
            this.dialog.article = true
            this.$nextTick(() => {
                this.$refs.articleDialog.open(4, row)
            })
        },
         //词串管理
         table_chapter(row) {
			this.dialog.chapter = true
            this.$nextTick(() => {
				this.$refs.chapterDialog.open(row)
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        },
        handVocabularyClose() {
            this.dialog.vocabulary = false
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
.position {
    display: flex;
    align-items: center;
}
.position-item {
    margin-right: 5px;
}
.position-item:last-child {
    margin-right: 0;
}
</style>
